<?php
/**
 * Created by PhpStorm.
 * User: yui
 * Date: 2017/3/30
 * Time: 22:40
 */

namespace api\controllers;

use Yii;
use common\controllers\ApiCommonCtrl;
use common\services\MemberAddressSve;

class MemberAddressController extends ApiCommonCtrl
{

    public function actionList()
    {
        $this->checkLogin();

        $MemberAddressSve = new MemberAddressSve();
        return $MemberAddressSve->getList([
            'member_id' => $this->TokenObj->member_id
        ], 'is_default DESC, member_address_id DESC', true);
    }

    /**
     * 删除地址
     * @return array
     */
    public function actionRemove()
    {
        $this->checkLogin();

        $MemberAddressSve = new MemberAddressSve();
        $id = Yii::$app->request->get('id', 0);
        return $MemberAddressSve->remove([
            'member_id' => $this->TokenObj->member_id,
            'member_address_id' => $id,
        ]);
    }

    /**
     * 查看地址信息
     * @return array
     */
    public function actionInfo()
    {
        $this->checkLogin();

        $MemberAddressSve = new MemberAddressSve();
        $id = Yii::$app->request->get('id', 0);
        return $MemberAddressSve->getInfo([
            'member_id' => $this->TokenObj->member_id,
            'member_address_id' => $id,
        ]);
    }

    public function actionAdd()
    {
        $this->checkLogin();

        $MemberAddressSve = new MemberAddressSve();
        return $MemberAddressSve->add(Yii::$app->request->post());
    }

    /**
     * 获取默认地址
     */
    public function actionGetDefault()
    {
        $this->checkLogin();

        $MemberAddressSve = new MemberAddressSve();
        return $MemberAddressSve->getDefault($this->Member);
    }

    /**
     * 设置默认地址
     */
    public function actionSetDefault()
    {
        $this->checkLogin();

        $MemberAddressSve = new MemberAddressSve();
        $id = Yii::$app->request->get('id', 0);
        return $MemberAddressSve->setDefault($this->Member, $id);
    }
}